package com.leejxx.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leejxx.article.dao.ApArticleConfigDao;
import com.leejxx.article.dao.ApArticleContentDao;
import com.leejxx.article.dao.ApArticleDao;
import com.leejxx.article.dao.AuthorDao;
import com.leejxx.article.service.ApArticleService;
import com.leejxx.common.constants.article.ArticleConstants;
import com.leejxx.model.article.dtos.ArticleDto;
import com.leejxx.model.article.dtos.ArticleHomeDto;
import com.leejxx.model.article.dtos.ArticleInfoDto;
import com.leejxx.model.article.pojos.ApArticle;
import com.leejxx.model.article.pojos.ApArticleConfig;
import com.leejxx.model.article.pojos.ApArticleContent;
import com.leejxx.model.article.pojos.ApAuthor;
import com.leejxx.model.common.dto.ResponseResult;
import com.leejxx.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleDao, ApArticle> implements ApArticleService {
    @Autowired
    private AuthorDao authorDao;

    @Autowired
    private ApArticleContentDao apArticleContentDao;

    @Autowired
    private ApArticleConfigDao apArticleConfigDao;

    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;

    @Autowired
    private ApArticleDao apArticleDao;

    @Value("${fdfs.url}")
    private String fileServerUrl;

    @Override
    public ResponseResult saveArticle(ArticleDto articleDto) {
        //1.检查参数
        if (articleDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(articleDto, apArticle);
        //2.保存或修改文章
        if (articleDto.getId() == null) {
            //补全文章作者id
            ApAuthor apAuthor = authorDao.selectOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, apArticle.getAuthorName()));
            if (apAuthor != null) {
                apArticle.setAuthorId(apAuthor.getId().longValue());
            }
            //保存文章
            save(apArticle);

            //保存文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setContent(articleDto.getContent());
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContentDao.insert(apArticleContent);

            //保存文章配置
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setIsForward(true);
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfigDao.insert(apArticleConfig);
        } else {
            //3.修改文章
            ApArticle article = getById(apArticle.getId());
            if (article == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章没找到");
            }
            //修改文章
            updateById(apArticle);
            //修改文章内容
            ApArticleContent apArticleContent = apArticleContentDao.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));
            apArticleContent.setContent(articleDto.getContent());
            apArticleContentDao.updateById(apArticleContent);
        }
        return ResponseResult.okResult(apArticle.getId());
    }

    @Override
    public ResponseResult load(Short loadType, ArticleHomeDto dto) {
        //1.校验参数
        Integer size = dto.getSize();
        if(size == null || size == 0){
            size = 10;
        }
        size = Math.min(size,MAX_PAGE_SIZE);
        dto.setSize(size);

        //类型参数检验
        if(!loadType.equals(ArticleConstants.LOADTYPE_LOAD_MORE)&&!loadType.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){
            loadType = ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //文章频道校验
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }

        //时间校验
        if(dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if(dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }
        //2.查询数据
        List<ApArticle> apArticles = apArticleDao.loadArticleList(dto, loadType);

        //3.结果封装
        ResponseResult responseResult = ResponseResult.okResult(apArticles);
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        Map<String,Object> resultMap = new HashMap<>();

        //1.检查参数
        if(dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.查询文章的配置
        ApArticleConfig apArticleConfig = apArticleConfigDao.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if(apArticleConfig == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(!apArticleConfig.getIsDown() && !apArticleConfig.getIsDelete()){
            //3.查询文章的内容
            ApArticleContent apArticleContent = apArticleContentDao.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
            resultMap.put("content",apArticleContent);
        }
        resultMap.put("config",apArticleConfig);

        //4.结果返回
        return ResponseResult.okResult(resultMap);
    }
}